package vn.vietel.pms.business;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;

import org.springframework.transaction.annotation.Transactional;

import vn.vietel.pms.entity.VHistoricalAction;
import vn.vietel.pms.service.HistorialActionService;

public class HistorialActionServiceImpl implements HistorialActionService {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public void create(VHistoricalAction item) {
		if (item != null) {
			entityManager.persist(item);
		}
	}

	@Transactional
	public VHistoricalAction find(Long id) {
		return entityManager.find(VHistoricalAction.class, id);
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	@Transactional
	public void delete(VHistoricalAction item) {
		entityManager.remove(item);

	}

	@Transactional
	public VHistoricalAction find(String hisName) {
		try {
			return (VHistoricalAction) entityManager
					.createQuery(
							"select g from VHistoricalAction g where g.actionname=:hisname")
					.setParameter("hisname", hisName).getSingleResult();
		} catch (PersistenceException e) {
			return null;
		}
	}

	@Transactional
	public VHistoricalAction find(String hisName, String hisDesc) {
		try {
			return (VHistoricalAction) entityManager
					.createQuery(
							"select g from VHistoricalAction g where g.actionname=:hisname and g.actionDesc=:hisDesc")
					.setParameter("kindname", hisName).setParameter(
							"kindDesc", hisDesc).getSingleResult();
		} catch (PersistenceException e) {
			return null;
		}
	}

	@Transactional
	public void update(VHistoricalAction item) {
		entityManager.merge(item);

	}

	@SuppressWarnings("unchecked")
	public List<VHistoricalAction> getAll() {

		try {
			javax.persistence.Query qr = entityManager
					.createQuery("select g from VHistoricalAction g ");
			return qr.getResultList();

		} catch (PersistenceException e) {
			return null;
		}
	}

}
